﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace XmlVisualizer.Controls {
	/// <summary>
	/// Interaction logic for ucEditableTextBlock.xaml
	/// </summary>
	public partial class ucEditableTextBlock : UserControl {
		#region Constructors
		/// <summary>
		/// Initializes a new instance of the ucEditableTextBlock.
		/// </summary>
		public ucEditableTextBlock() {
			InitializeComponent();
			base.Focusable = true;
			base.FocusVisualStyle = null;
		}
		#endregion

		#region Member fields
		// We keep the old text when we go into editmode
		// in case the user aborts with the escape key
		private string oldText;
		#endregion

		#region Dependency properties
		/// <summary>
		/// Gets or sets the TextProperty.
		/// </summary>
		public string Text {
			get { return (string)GetValue(TextProperty); }
			set { SetValue(TextProperty, value); }
		}

		/// <summary>
		/// Identifies the XmlVisualizer.Controls.ucEditableTextBlock.TextProperty.
		/// </summary>
		public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text",
			typeof(string), typeof(ucEditableTextBlock), new PropertyMetadata(""));

		/// <summary>
		/// Gets or sets the IsEditableProperty.
		/// </summary>
		public bool IsEditable {
			get { return (bool)GetValue(IsEditableProperty); }
			set { SetValue(IsEditableProperty, value); }
		}

		/// <summary>
		/// Identifies the XmlVisualizer.Controls.ucEditableTextBlock.IsEditableProperty.
		/// </summary>
		public static readonly DependencyProperty IsEditableProperty = DependencyProperty.Register("IsEditable",
			typeof(bool), typeof(ucEditableTextBlock), new PropertyMetadata(true));

		/// <summary>
		/// Gets or sets the IsInEditModeProperty.
		/// </summary>
		public bool IsInEditMode {
			get {
				if (IsEditable)
					return (bool)GetValue(IsInEditModeProperty);
				else
					return false;
			}
			set {
				if (IsEditable) {
					if (value)
						oldText = Text;
					SetValue(IsInEditModeProperty, value);
				}
			}
		}

		/// <summary>
		/// Identifies the XmlVisualizer.Controls.ucEditableTextBlock.IsInEditModeProperty.
		/// </summary>
		public static readonly DependencyProperty IsInEditModeProperty = DependencyProperty.Register("IsInEditMode",
			typeof(bool), typeof(ucEditableTextBlock), new PropertyMetadata(false));

		/// <summary>
		/// Gets or sets the TextFormatProperty.
		/// </summary>
		public string TextFormat {
			get { return (string)GetValue(TextFormatProperty); }
			set {
				if (value == "")
					value = "{0}";
				SetValue(TextFormatProperty, value);
			}
		}

		/// <summary>
		/// Identifies the XmlVisualizer.Controls.ucEditableTextBlock.TextFormatProperty.
		/// </summary>
		public static readonly DependencyProperty TextFormatProperty = DependencyProperty.Register("TextFormat",
			typeof(string), typeof(ucEditableTextBlock), new PropertyMetadata("{0}"));

		#endregion

		#region Properties
		/// <summary>
		/// Gets the FormattedText.
		/// </summary>
		public string FormattedText {
			get { return String.Format(TextFormat, Text); }
		}
		#endregion

		#region Event handlers
		/// <summary>
		/// Invoked when we enter edit mode.
		/// </summary>
		/// <param name="sender">The object where the event handler is attached.</param>
		/// <param name="e">The event data.</param>
		void TextBox_Loaded(object sender, RoutedEventArgs e) {
			TextBox txt = sender as TextBox;

			// Give the TextBox input focus
			txt.Focus();
			txt.SelectAll();
		}

		/// <summary>
		/// Invoked on MouseLeftButtonDown.
		/// </summary>
		/// <param name="e">The event data.</param>
		protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) {
			base.OnMouseLeftButtonDown(e);
			if (IsInEditMode) {
				this.IsInEditMode = true;
				e.Handled = true;
			}
		}

		/// <summary>
		/// Invoked on MouseDoubleClick.
		/// </summary>
		/// <param name="e"></param>
		protected override void OnMouseDoubleClick(MouseButtonEventArgs e) {
			base.OnMouseDoubleClick(e);
			this.IsInEditMode = true;
		}

		/// <summary>
		/// Invoked when we exit edit mode.
		/// </summary>
		/// <param name="sender">The object where the event handler is attached.</param>
		/// <param name="e">The event data.</param>
		void TextBox_LostFocus(object sender, RoutedEventArgs e) {
			this.IsInEditMode = false;
		}

		/// <summary>
		/// Invoked when the user edits the annotation.
		/// </summary>
		/// <param name="sender">The object where the event handler is attached.</param>
		/// <param name="e">The event data.</param>
		void TextBox_KeyDown(object sender, KeyEventArgs e) {
			if (e.Key == Key.Enter) {
				this.IsInEditMode = false;
				e.Handled = true;
			} else if (e.Key == Key.Escape) {
				this.IsInEditMode = false;
				Text = oldText;
				e.Handled = true;
			}
		}
		#endregion
	}
}
